<?php
class Mod_users extends CI_Model {

	/*
	 Attempts to login user and set session. Returns boolean based on outcome.
	 */
	function login($email, $password) {
		$query = $this -> db -> get_where(PREFIX_TABLE . 'users', array('email' => $email, 'password' => md5($password), 'status' => 1), 1);
		if ($query -> num_rows() == 1) {
			$row = $query -> row();
			$this -> session -> set_userdata('authentication_id', $row -> user_id);
			$this -> session -> set_userdata('aut_group_id', $row -> use_user_group_id);
			return true;
		}
		return false;
	}

	/*
	 Logs out a user by destorying all session data and redirect to login
	 */
	function logout() {
		$this -> session -> sess_destroy();
		redirect('ctme-common');
	}

	/*
	 Determins if a user is logged in
	 */
	function is_logged_in() {
		return $this -> session -> userdata('authentication_id') != false;
	}

	/*
	 Gets information about the currently logged in user.
	 */
	function get_logged_in_user_info() {
		if ($this -> is_logged_in()) {
			return $this -> get_info($this -> session -> userdata('authentication_id'));
		}

		return false;
	}

	/*
	 Determins whether the user specified user has access the specific module.
	 */
	function has_permission($module_name, $user_id) {
		//if no module_id is null, allow access
		if ($module_name == null) {
			return true;
		}
		$query_module_name = $this -> db -> get_where(PREFIX_TABLE . 'modules', array('name_lang_key' => "module_" . str_replace('-', '_', $module_name)), 1);
		$module_id = $query_module_name -> row() -> module_id;

		$query = $this -> db -> get_where(PREFIX_TABLE . 'user_access_permissions', array('use_user_id' => $user_id, 'use_module_id' => $module_id), 1);
		return $query -> num_rows() == 1;

		return false;
	}

	/*
	 Determins whether the user specified user group has access the specific module.
	 */
	function has_permission_group($module_name, $group_id) {
		//if no module_id is null, allow access
		if ($module_name == null) {
			return true;
		}
		$query_module_name = $this -> db -> get_where(PREFIX_TABLE . 'modules', array('name_lang_key' => "module_" . str_replace('-', '_', $module_name)), 1);
		$module_id = $query_module_name -> row() -> module_id;

		$query = $this -> db -> get_where(PREFIX_TABLE . 'group_access_permissions', array('use_user_group_id' => $group_id, 'use_module_id' => $module_id), 1);
		return $query -> num_rows() == 1;

		return false;
	}

	/*
	 Determins whether the user specified user has modify the specific module.
	 */
	function is_has_modify_permission($module_name, $user_id) {
		//if no module_id is null, allow access
		if ($module_name == null) {
			return true;
		}
		$query_module_name = $this -> db -> get_where(PREFIX_TABLE . 'modules', array('name_lang_key' => "module_" . str_replace('-', '_', $module_name)), 1);
		if ($query_module_name -> num_rows() <= 0) {
			return FALSE;
		}
		$module_id = $query_module_name -> row() -> module_id;

		$query = $this -> db -> get_where(PREFIX_TABLE . 'user_modify_permissions', array('use_user_id' => $user_id, 'use_module_id' => $module_id), 1);
		return $query -> num_rows() == 1;

		return false;
	}

	/*
	 Determins whether the user specified user group has modify the specific module.
	 */
	function is_has_modify_permission_group($module_name, $group_id) {
		//if no module_id is null, allow access
		if ($module_name == null) {
			return true;
		}
		$query_module_name = $this -> db -> get_where(PREFIX_TABLE . 'modules', array('name_lang_key' => "module_" . str_replace('-', '_', $module_name)), 1);
		if ($query_module_name -> num_rows() <= 0) {
			return FALSE;
		}
		$module_id = $query_module_name -> row() -> module_id;

		$query = $this -> db -> get_where(PREFIX_TABLE . 'group_modify_permissions', array('use_user_group_id' => $group_id, 'use_module_id' => $module_id), 1);
		return $query -> num_rows() == 1;

		return false;
	}

	/*
	 Gets information about a particular user
	 */
	function get_info($user_id) {
		$this -> db -> from(PREFIX_TABLE . 'users');
		//$this->db->join('people', 'people.person_id = users.person_id');
		$this -> db -> where(PREFIX_TABLE . 'users.user_id', $user_id);
		$query = $this -> db -> get();

		if ($query -> num_rows() == 1) {
			return $query -> row();
		} else {
			//Get empty base parent object, as $user_id is NOT an user
			$user_obj = parent::get_info(-1);

			//Get all the fields from user table
			$fields = $this -> db -> list_fields('users');

			//append those fields to base parent object, we we have a complete empty object
			foreach ($fields as $field) {
				$user_obj -> $field = '';
			}

			return $user_obj;
		}
	}

}
